package com.mall.server.service.sms;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.SmsCouponApi;
import com.mall.dubbo.api.SmsCouponProductCategoryRelationApi;
import com.mall.dubbo.api.SmsCouponProductRelationApi;
import com.mall.model.domain.SmsCoupon;
import com.mall.model.domain.SmsCouponProductCategoryRelation;
import com.mall.model.domain.SmsCouponProductRelation;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.SmsCouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsCouponService {

    @DubboReference
    private SmsCouponApi smsCouponApi;

    @DubboReference
    private SmsCouponProductCategoryRelationApi smsCouponProductCategoryRelationApi;

    @DubboReference
    private SmsCouponProductRelationApi smsCouponProductRelationApi;


    //根据优惠券名称和类型分页获取优惠券列表
    public CommonPage list(Integer pageNum, Integer pageSize, SmsCoupon smsCoupon) {

        String name = smsCoupon.getName();

        Integer type = smsCoupon.getType();

        Page<SmsCoupon> page = smsCouponApi.listPage(pageNum, pageSize, name, type);

        return new CommonPage(page);
    }

    //获取单个优惠券的详细信息
    public SmsCouponVo findById(Integer id) {

        SmsCoupon smsCoupon = smsCouponApi.getById(id);
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponProductCategoryRelationApi.findByCouponId(id);
        List<SmsCouponProductRelation> productRelationList = smsCouponProductRelationApi.findByCouponId(id);

        return SmsCouponVo.init(smsCoupon
                , productCategoryRelationList, productRelationList);

    }

    //删除优惠券
    public void delete(Integer id) {

        smsCouponApi.removeById(id);
        smsCouponProductCategoryRelationApi.deleteByCouponId(id);
        smsCouponProductRelationApi.deleteByCouponId(id);

    }

    //添加优惠券
    public void create(SmsCouponVo smsCouponVo) {

        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponVo.getProductCategoryRelationList();
        List<SmsCouponProductRelation> productRelationList = smsCouponVo.getProductRelationList();

        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(smsCouponVo, smsCoupon);
        Long id = smsCouponApi.saveCoupon(smsCoupon);

        if (CollUtil.isNotEmpty(productCategoryRelationList)) {
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelation.setCouponId(id);
            }
            smsCouponProductCategoryRelationApi.saveBatch(productCategoryRelationList);
        }
        if (CollUtil.isNotEmpty(productRelationList)) {
            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelation.setCouponId(id);
            }
            smsCouponProductRelationApi.saveBatch(productRelationList);
        }

    }


    //修改优惠券
    public void updateCoupon(Integer id, SmsCouponVo smsCouponVo) {

        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponVo.getProductCategoryRelationList();
        List<SmsCouponProductRelation> productRelationList = smsCouponVo.getProductRelationList();
        if (CollUtil.isNotEmpty(productCategoryRelationList)) {
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelationApi.updateCategoryRelation(id, smsCouponProductCategoryRelation);
            }
        }
        if (CollUtil.isNotEmpty(productRelationList)) {
            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelationApi.updateProductRelation(id, smsCouponProductRelation);
            }
        }
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(smsCouponVo, smsCoupon);

        smsCouponApi.updateCoupon(id, smsCoupon);

    }

}
